<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch } from 'vue';

import { useResizeObserver } from '@vueuse/core';
import * as echarts from 'echarts';

const props = defineProps({
  chartData: {
    type: Object,
    default: () => ({}),
  },
});

const chartRef = ref<HTMLDivElement | null>(null);
let chart: echarts.ECharts | null = null;

const initChart = () => {
  if (!chartRef.value) return;
  chart = echarts.init(
    chartRef?.value.querySelector('#horizontalChart') as HTMLElement,
  );

  const option = {
    backgroundColor: 'transparent',
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      top: '10%',
      left: '5%',
      right: '10%',
      bottom: '5%',
      containLabel: true,
    },
    legend: {
      // 修改这里，确保legend.data与series中的name完全一致
      data: ['激活人数', '会员人数'],
      textStyle: {
        color: '#fff',
      },
      right: 10,
      top: 0,
      formatter: (name: string) => {
        const total =
          name === '激活人数'
            ? props.chartData.totalActive
            : props.chartData.totalMember;
        return `${name}${total}`;
      },
    },
    xAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#177ddc',
        },
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(23, 125, 220, 0.2)',
        },
      },
      axisLabel: {
        color: '#fff',
      },
    },
    yAxis: {
      type: 'category',
      data: props.chartData.units,
      axisLine: {
        lineStyle: {
          color: '#177ddc',
        },
      },
      axisLabel: {
        color: '#fff',
        fontSize: 12,
      },
    },
    series: [
      {
        name: '激活人数',
        type: 'bar',
        stack: 'total',
        data: props.chartData.activeData,
        barWidth: 8,
        z: 2,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: '#FF3B3B' },
            { offset: 1, color: '#FF534D' },
          ]),
        },
        label: {
          show: true,
          position: 'right',
          offset: [5, 0],
          color: '#fff',
          formatter: '{c}',
          fontSize: 12,
        },
      },
      {
        name: '会员人数',
        type: 'bar',
        stack: 'total',
        data: props.chartData.memberData,
        barWidth: 8,
        z: 1,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: '#00B4FF' },
            { offset: 1, color: '#0086FF' },
          ]),
        },
        label: {
          show: true,
          position: 'right',
          offset: [5, 0],
          color: '#fff',
          formatter: '{c}',
          fontSize: 12,
        },
      },
    ],
  };

  chart.setOption(option);
};

// 监听数据变化
watch(
  () => props.chartData,
  () => {
    initChart();
  },
  { deep: true },
);

// 监听容器大小变化
useResizeObserver(chartRef, () => {
  chart?.resize();
});

onMounted(() => {
  initChart();
});

onUnmounted(() => {
  chart?.dispose();
});
</script>

<template>
  <div class="chart-container" ref="chartRef">
    <div class="chart" id="horizontalChart"></div>
  </div>
</template>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
  border-radius: 4px;
}

.chart {
  width: 100%;
  height: 100%;
  padding: 12px;
}
</style>
